package com.zhongen.oa.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.zhongen.admin.api.entity.SysUser;
import com.zhongen.oa.entity.*;
import com.zhongen.oa.entity.ProjectRequirementEntity;
import com.zhongen.oa.entity.vo.ProjectRequirementVo;
import com.zhongen.oa.mapper.ProjectRequirementMapper;
import com.zhongen.oa.service.ProjectBasicInfoService;
import com.zhongen.oa.service.ProjectRequirementService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 项目需求表
 *
 * @author Code Generator
 * @date 2024-01-12 09:43:59
 */
@Service
@AllArgsConstructor
public class ProjectRequirementServiceImpl extends ServiceImpl<ProjectRequirementMapper, ProjectRequirementEntity>
		implements ProjectRequirementService {

	private final ProjectBasicInfoService projectBasicInfoService;

	@Override
	public IPage<ProjectRequirementVo> getProjectRequirementPage(Page page,
			ProjectRequirementEntity projectRequirement) {
		MPJLambdaWrapper<ProjectRequirementEntity> wrapper = new MPJLambdaWrapper<ProjectRequirementEntity>()
			.selectAll(ProjectRequirementEntity.class)
			.select(ProjectBasicInfoEntity::getProjectName, ProjectBasicInfoEntity::getProjectNumber)
			.select(SysUser::getName)
			.leftJoin(SysUser.class, SysUser::getUserId, ProjectRequirementEntity::getCommander)
			.leftJoin(ProjectBasicInfoEntity.class, ProjectBasicInfoEntity::getId,
					ProjectRequirementEntity::getProjectId);
		wrapper.eq(StrUtil.isNotBlank(projectRequirement.getRequirementStatus()),
				ProjectRequirementEntity::getRequirementStatus, projectRequirement.getRequirementStatus());
		wrapper.eq(projectRequirement.getProjectId() != null && projectRequirement.getProjectId() != 0,
				ProjectRequirementEntity::getProjectId, projectRequirement.getProjectId());

		wrapper.and(StrUtil.isNotBlank(projectRequirement.getRequirementNumber()),
				wrapper2 -> wrapper2
					.like(StrUtil.isNotBlank(projectRequirement.getRequirementName()),
							ProjectRequirementEntity::getRequirementName, projectRequirement.getRequirementName())
					.or()
					.like(StrUtil.isNotBlank(projectRequirement.getRequirementNumber()),
							ProjectRequirementEntity::getRequirementNumber, projectRequirement.getRequirementNumber()));
		return baseMapper.selectJoinPage(page, ProjectRequirementVo.class, wrapper);

	}

	@Override
	public boolean addProjectRequirement(ProjectRequirementEntity projectRequirement) {
		// 根据项目ID查询项目信息
		ProjectBasicInfoEntity projectBasicInfo = projectBasicInfoService.getById(projectRequirement.getProjectId());
		// 根据项目缩写查询项目需求序号
		Long count = baseMapper.selectCount(
				new QueryWrapper<ProjectRequirementEntity>().eq("project_id", projectRequirement.getProjectId()));
		// 项目缩写+序号组成需求编号
		projectRequirement.setRequirementNumber(projectBasicInfo.getProjectNumber() + "-XQ-" + (count + 1));
		baseMapper.insert(projectRequirement);
		return true;
	}

	@Override
	public ProjectRequirementVo getById(Long id) {
		return baseMapper.getByid(id);
	}

	@Override
	public List<ProjectRequirementVo> exportList(ProjectRequirementEntity projectRequirement, Long[] ids) {
		return baseMapper.exportList(projectRequirement, ids);
	}

}
